use std::ffi::CString;
use crate::types::ParameterNameRef;
use crate::VoicemeeterRemote;
impl VoicemeeterRemote {
#[tracing::instrument(skip(self))]
pub fn set_parameter_float(
&self,
param: &ParameterNameRef,
value: f32,
) -> Result<(), SetParameterError> {
let param = CString::new(param.as_ref()).unwrap();
tracing::debug!("setting float parameter");
let res = unsafe {
self.raw
.VBVMR_SetParameterFloat(param.as_ptr() as *mut _, value)
};
match res {
0 => Ok(()),
-1 => Err(SetParameterError::CannotGetClient),
-2 => Err(SetParameterError::NoServer),
-3 => Err(SetParameterError::UnknownParameter(
param.to_string_lossy().into_owned(),
)), s => Err(SetParameterError::Other(s)),
}
}
#[tracing::instrument(skip(self))]
pub fn set_parameter_string(
&self,
param: &ParameterNameRef,
value: &str,
) -> Result<(), SetParameterError> {
let param = CString::new(param.as_ref()).unwrap();
let value = CString::new(value).unwrap();
tracing::debug!("setting string parameter");
let res = unsafe {
self.raw
.VBVMR_SetParameterStringA(param.as_ptr() as *mut _, value.as_ptr() as *mut _)
};
match res {
0 => Ok(()),
-1 => Err(SetParameterError::CannotGetClient),
-2 => Err(SetParameterError::NoServer),
-3 => Err(SetParameterError::UnknownParameter(
param.to_string_lossy().into_owned(),
)), s => Err(SetParameterError::Other(s)),
}
}
pub fn set_parameters(&self, script: &str) -> Result<(), SetParametersError> {
let script = CString::new(script).unwrap();
let res = unsafe { self.raw.VBVMR_SetParameters(script.as_ptr() as *mut _) };
match res {
l if l > 0 => Err(SetParametersError::ScriptError(l as usize)),
0 => Ok(()),
-1 => Err(SetParameterError::CannotGetClient.into()),
-2 => Err(SetParameterError::NoServer.into()),
s => Err(SetParameterError::Other(s).into()),
}
}
}
#[derive(Debug, thiserror::Error, Clone)]
#[non_exhaustive]
pub enum SetParametersError {
#[error("script error on line: {0}")]
ScriptError(usize),
#[error(transparent)]
SetParameterError(#[from] SetParameterError),
}
#[derive(Debug, thiserror::Error, Clone)]
#[non_exhaustive]
pub enum SetParameterError {
#[error("cannot get client (unexpected)")]
CannotGetClient,
#[error("no server")]
NoServer,
#[error("unknown parameter: {0}")]
UnknownParameter(String),
#[error("unexpected error occurred: error code {0}")]
Other(i32),
}